#ifndef _M_DATA_HANDLE_
#define _M_DATA_HANDLE_
#include "stdint.h"
#include "stdbool.h"
#include "data_handle.h"

#define BEAN_NAME_LEN_MAX   13      //跑步豆名称长度

#pragma pack (1)
//上课、训练状态
typedef struct
{
	uint8_t     pe_state;           //上课状态 0x00:未上课 0x01:上课中 0x02:上课完成待同步 0x03:同步中 0x04:同步完成
	uint8_t     train_state;        //训练状态 0x00:无训练 0x01:训练中 0x02:训练完成
	uint8_t     train_id;           //训练id
	uint8_t     train_level;        //训练等级 0x01:等级A  0x01:等级B  0x01:等级C
    uint16_t    train_num_OneStep;  //每组训练数量
	uint8_t     train_all_step;     //训练总步骤
	uint8_t     train_cur_step;     //训练当前步骤
	uint16_t    train_sec;          //训练时长；单位：秒
}bracelet_real_state_t;

//指令下发
typedef struct
{
	uint16_t frame_head;
	uint8_t  sync_id;  
	uint16_t total_cnt;  
}sync_cmd_t;

/* 消息队列 */
typedef struct
{
    uint8_t         u8Event;
    uint8_t         u8Parameter;
    uint8_t*        pdata; 
}TRAIN_EVENT_MSG;

/* 消息 */
enum
{
    MSG_CLASS_START = 1,
    MSG_CLASS_END,
	MSG_SYNC_START,              /* 同步开始事件 */ 
	MSG_SYNC_OVER,               /* 同步完成数据 */ 
	MSG_SYNC_STOP,               /* 同步停止数据 */ 
};

typedef struct
{
    uint16_t    total_distance;         //距离
    uint16_t    total_step;             //步数
    uint16_t    total_calorie;          //卡路里
    uint16_t    pace;                   //配速
    uint8_t     step_frequency;         //步频
    uint8_t     step_length;            //步幅
    uint8_t     heart_rate_interval;    //心率区间
}sports_data;

typedef struct
{
    uint8_t     flag_ware:1;        //佩戴标识
    uint8_t     flag_RB_con:1;      //跑步豆连接标识
    uint8_t     flag_GPS:1;         //GPS连接标识
    uint8_t     res:5;              //步频
}belt_flag;

typedef struct
{
    uint16_t    u16FirstStart;                          //首次使用标志；首次使用，此处设置为AACC
    uint8_t     u8BeanPaired;                           //是否已经匹配过跑步豆；1：已经匹配； 0：未匹配
    uint8_t     u8BeanName[BEAN_NAME_LEN_MAX];          //已经匹配过的跑步豆名称；
    uint16_t    belt_id;                                //手表id
} SYSTEM_INFO_T;

typedef struct
{
    uint8_t         version;            //版本号
    uint16_t        belt_id;            //手环ID
    belt_flag       flag;               //标识
    uint8_t         bat_percent;        //电量
    uint8_t         train_id;           //训练ID
    uint8_t         heart_rate;         //心率
    uint8_t         data_len;           //数据长度
    sports_data     data;               //运动数据
    uint16_t        sync_data_cnt;      //同步数据条数
}adv_data_t;

typedef struct
{
    uint16_t        header;             //帧头
    uint8_t         version;            //版本号
    uint16_t        belt_id;            //手环ID
    belt_flag       flag;               //标识
    uint8_t         bat_percent;        //电量
    uint8_t         train_id;           //训练ID
    uint8_t         heart_rate;         //心率
    uint8_t         data_len;           //数据长度
    sports_data     data;               //运动数据
    uint16_t        sync_data_cnt;      //同步数据条数
}ble_uart_data_t;

typedef struct
{
    uint16_t        header;             //帧头
    uint8_t         version;            //版本号
    uint16_t        belt_id;            //手环ID
    uint8_t         sync_id;            //标识
    uint16_t        packet_num;         //电量
    uint8_t         heart_rate;         //心率
    uint8_t         data_len;           //数据长度
    sports_data     data;               //运动数据
    uint16_t        sync_data_cnt;      //同步数据条数
}ble_sync_data_t;

#pragma pack()

void receive_cmd_handler(uint8_t* pData,uint16_t len);
void class_start(void);
void class_end(void);
void synchronize_data_start(void);
void synchronize_data_over(void);
void prepare_adv_data(void);
void prepare_ble_uart_data(void);

void task_train_Message_Send(TRAIN_EVENT_MSG Message);
void task_train_Message_SendFromISR(TRAIN_EVENT_MSG Message);
void task_train_handle(void * arg);

extern bracelet_real_state_t m_bracelet_state;//课程，训练状态
extern uint32_t g_sync_addr;

extern uint8_t test_send_data[240];
extern sync_cmd_t g_sync_cmd_buf;

extern uint8_t sync_flag;       //同步数据启停标志位
extern uint8_t gps_connect_flag;
extern uint8_t save_data_flag;
extern bool allow_sleep(void);

extern SYSTEM_INFO_T g_sysmng_info;
extern adv_data_t g_adv_data_buf;
extern uint16_t   g_adv_data_buf_size;
extern ble_uart_data_t g_ble_uart_buf ;
extern uint16_t        g_ble_uart_buf_size;
extern ble_sync_data_t g_ble_sync_buf;
extern uint16_t        g_ble_sync_buf_size;

extern uint32_t g_sync_total_cnt_finished;
#endif
